﻿<script type="text/html" template>
    <title>{{layui.setter.MenuName}}-{{layui.setter.name}}</title>
</script>
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <script type="text/html" template>
            <a><cite>{{layui.setter.MenuName}}</cite></a>
        </script>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="app-content-list">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">优惠券名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="couponName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class='layui-form-label'>优惠卷类型 </label>
                    <div class='layui-input-block'>
                        <div class='layui-input-inline'>
                            <select name='CType_Sel' lay-filter='CType_Sel'>
                                <option value='-1' {{ d.params.Type_Sel == '-1' ? 'selected' : '' }}>请选择</option>
                                <option value='0' {{ d.params.Type_Sel == '0' ? 'selected' : '' }}>商品优惠卷</option>
                                <option value='1' {{ d.params.Type_Sel == '1' ? 'selected' : '' }}>店铺优惠卷</option>
                                <option value='2' {{ d.params.Type_Sel == '2' ? 'selected' : '' }}>平台优惠卷</option>
                                <option value='3' {{ d.params.Type_Sel == '3' ? 'selected' : '' }}>新人优惠卷</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class='layui-form-label'>发送方式</label>
                    <div class='layui-input-block'>
                        <div class='layui-input-inline'>
                            <select name='Take_Sel' lay-filter='Take_Sel'>
                                <option value='-1' {{ d.params.Type_Sel == '-1' ? 'selected' : '' }}>请选择</option>
                                <option value='0' {{ d.params.Type_Sel == '0' ? 'selected' : '' }}>后台发送</option>
                                <option value='1' {{ d.params.Type_Sel == '1' ? 'selected' : '' }}>客户自领</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">会员名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="userName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class='layui-form-label'>是否使用 </label>
                    <div class='layui-input-block'>
                        <div class='layui-input-inline'>
                            <select name='Use_sel' lay-filter='Use_sel'>
                                <option value='-1' {{ d.params.Type_Sel == '-1' ? 'selected' : '' }}>请选择</option>
                                <option value='0' {{ d.params.Type_Sel == '0' ? 'selected' : '' }}>未使用</option>
                                <option value='1' {{ d.params.Type_Sel == '1' ? 'selected' : '' }}>使用</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <script type="text/html" template
                    lay-url="{{ layui.setter.APIUrl }}/shop/coupon_memberMgr/GetButtonRight" lay-type="post"
                    lay-done="layui.data.setActive()">
                    {{ d.Data }}
                </script>
            </div>
            <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
        </div>
    </div>
</div>
<script>


    layui.use('shop_coupon_member', layui.factory('shop_coupon_member')).use(['admin', 'table', 'form'], function () {

        var $ = layui.$
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , form = layui.form;

        form.render(null, 'app-content-list');

        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('LAY-app-content-list', {
                where: field,
                page: {
                    curr: 1 //重新从第1页开始
                }
            });
        });
    });

    layui.data.setActive = function () {

        layui.use(['form', 'admin', 'table'], function () {
            var $ = layui.$;
            var form = layui.form;
            var admin = layui.admin;
            var view = layui.view;
            var table = layui.table;
            var active = {
                del: function () {
                    var checkStatus = table.checkStatus('LAY-app-content-list')
                        , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    var ids = "";
                    $.each(checkData, function (index, eobj) {
                        ids += "," + eobj.PID;
                    });
                    if (ids.indexOf(',') == 0)
                        ids = ids.substr(1);
                    //return layer.msg('已删除');
                    layer.confirm('确定删除吗？', function (index) {
                        admin.req({
                            url: layui.setter.APIUrl + '/shop/coupon_memberMgr/BatchDelete' //实际使用请改成服务端真实接口
                            , data: { "ids": ids }
                            , type: "post"
                            //, success: function (res) {
                            //}
                            , done: function (res) {
                                layer.close(index); //执行关闭
                                layer.msg(res.ResultInfo);
                                table.reload('LAY-app-content-list');
                            }
                        });
                    });
                },
                //添加
                add: function (othis) {
                    //location.hash = '/shop/shop_coupon_member/addChild';           
                    admin.popup({
                        title: '添加信息'
                        , skin: 'layui-layer-lan'
                        , area: ['850px', '650px']
                        , id: 'LAY-popup-content-add'
                        , success: function (layero, index) {
                            view(this.id).render('/shop/shop_coupon_member/add').done(function () {
                                form.render(null, 'layuiadmin-app-form-list');

                                //监听提交
                                form.on('submit(layuiadmin-app-form-submit)', function (data) {
                                    var field = data.field; //获取提交的字段
                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
                                    //请求登入接口
                                    admin.req({
                                        url: layui.setter.APIUrl + '/shop/coupon_memberMgr/Add' //实际使用请改成服务端真实接口
                                        , data: field
                                        , type: "post"
                                        , done: function (res) {
                                            layer.close(index); //执行关闭
                                            layui.table.reload('LAY-app-content-list'); //重载表格
                                            layer.msg('新增成功');
                                        }
                                    });
                                });
                            });
                        }
                    });
                },

                recover: function (othis) {
                    var checkStatus = table.checkStatus('LAY-app-content-list')
                        , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    var ids = "";
                    var isWoryState = false;

                    $.each(checkData, function (index, eobj) {
                        if (eobj.State == 3 || eobj.UseState == 1) {
                            return layer.msg('优惠券已经回收，或者已经使用不能再次回收！');
                            isWoryState = true;
                        }
                    });
                    if (isWoryState) { return; }

                    $.each(checkData, function (index, eobj) {
                        ids += "," + eobj.PID;
                    });
                    if (ids.indexOf(',') == 0)
                        ids = ids.substr(1);


                    layer.confirm('确定会回收吗？', function (index) {
                        admin.req({
                            url: layui.setter.APIUrl + '/Shop/Coupon_memberMgr/recover' //实际使用请改成服务端真实接口
                            , data: { "PID": ids }
                            , type: "post"
                            //, success: function (res) {
                            //}
                            , done: function (res) {
                                layer.close(index); //执行关闭 
                                table.reload('LAY-app-content-list');
                                layer.msg(res);
                            }
                        });
                    });
                },

                frost: function (othis) {
                    var checkStatus = table.checkStatus('LAY-app-content-list')
                        , checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }

                    var ids = "";
                    var isWoryState = false;

                    $.each(checkData, function (index, eobj) {
                        if (eobj.State != 1) {
                            return layer.msg('只有激活状态下的优惠券能冻结！');
                            isWoryState = true;
                        }
                    });
                    if (isWoryState) { return; }


                    var ids = "";
                    $.each(checkData, function (index, eobj) {
                        ids += "," + eobj.PID;
                    });
                    if (ids.indexOf(',') == 0)
                        ids = ids.substr(1);
                    layer.confirm('确定会冻结吗？', function (index) {
                        admin.req({
                            url: layui.setter.APIUrl + '/Shop/Coupon_memberMgr/frost' //实际使用请改成服务端真实接口
                            , data: { "PID": data.PID }
                            , type: "post"
                            , done: function (res) {
                                layer.close(index); //执行关闭 
                                table.reload('LAY-app-content-list');
                                layer.msg(res);
                            }
                        });
                    });
                },

                unFrost: function (othis) {
                    var checkStatus = table.checkStatus('LAY-app-content-list')
                        , checkData = checkStatus.data; //得到选中的数据
                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }

                    var ids = "";
                    var isWoryState = false;

                    $.each(checkData, function (index, eobj) {
                        if (eobj.State != 2) {
                            return layer.msg('只有冻结状态下的优惠券能解冻！');
                            isWoryState = true;
                        }
                    });
                    if (isWoryState) { return; }

                    var ids = "";
                    $.each(checkData, function (index, eobj) {
                        ids += "," + eobj.PID;
                    });
                    if (ids.indexOf(',') == 0)
                        ids = ids.substr(1);

                    layer.confirm('确定会解冻吗？', function (index) {
                        admin.req({
                            url: layui.setter.APIUrl + '/Shop/Coupon_memberMgr/unFrost' //实际使用请改成服务端真实接口
                            , data: { "PID": data.PID }
                            , type: "post"
                            , done: function (res) {
                                layer.close(index); //执行关闭 
                                table.reload('LAY-app-content-list');
                                layer.msg(res);
                            }
                        });
                    });
                }

            };

            $('.layui-btn.layuiadmin-btn-list').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    }

</script>